package per.yhh.xxxtsj.controller;


import org.springframework.web.bind.annotation.*;
import per.yhh.xxxtsj.entity.Message;
import per.yhh.xxxtsj.entity.User;
import per.yhh.xxxtsj.service.UserService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yhh
 * @since 2022-04-10
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    final
    UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/users")
    public Message users(@RequestParam("query") String keyword, @RequestParam("pagenum") Integer pageNum, @RequestParam("pagesize") Integer pageSize) {
        return Message.ok(userService.getUsersByPageAndKey(keyword, pageNum, pageSize));
    }

    @GetMapping("/getuser/{uid}")
    public Message getUser(@PathVariable("uid") Integer uid) {
        return Message.ok(userService.getUser(uid));
    }

    @PostMapping("/adduser")
    public Message addUser(@RequestBody User user) {
        if (userService.addUser(user)) {
            return Message.ok(null);
        }
        return Message.failed();
    }

    @GetMapping("/setpermission")
    public Message setPermission(@RequestParam("permissions") Integer permissions, @RequestParam("uid") Integer uid) {
        if (permissions <= 4 && permissions >= 0 && uid != null) {
            if (userService.setPermission(uid, permissions)) {
                return Message.ok(null);
            }
        }
        return Message.failed();
    }
}

